package top.roud.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import top.roud.bean.User;
import top.roud.dao.query.UserExits;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        String sysCode = (String) request.getSession().getAttribute("imgcode");
        String userCode = request.getParameter("verifycode");
        PrintWriter out = response.getWriter();
        String userphone = request.getParameter("userphone");
        String pwd = request.getParameter("password");
        User u = new User(userphone,pwd);
        if(userCode.toUpperCase().equals(sysCode.toUpperCase())) {
        	try {
				if(UserExits.userIsExists(u)) {
					request.getSession().setAttribute("user", u);
					response.sendRedirect("manage/manage.jsp?name=Roud");
				}else {
					request.setAttribute("msg", "账号或密码错误");
		        	request.getRequestDispatcher("WEB-INF/loginFail.jsp").forward(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }else {
        	request.setAttribute("msg", "验证码错误");
        	request.getRequestDispatcher("WEB-INF/loginFail.jsp").forward(request, response);
        }

    }
}
